package com.vcg.neo4j;

import org.junit.Before;
import org.junit.Test;
import org.neo4j.driver.v1.*;

/**
 * Created by wuyu on 2016/9/15.
 */
public class HelloWorld {

    Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "wknimagnim" ) );


    @Test
    public void test1(){

        Session session = driver.session();

        session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );

        StatementResult result = session.run( "MATCH p=shortestPath(\n" +
                "  (bacon:Person {name:\"Kevin Bacon\"})-[*]-(meg:Person {name:\"Meg Ryan\"})\n" +
                ")\n" +
                "RETURN p" );
        while ( result.hasNext() )
        {
            Record record = result.next();
            System.out.println( record.toString());
        }

        session.close();
        driver.close();
    }

    @Test
    public void test2(){
        Session session = driver.session();
        StatementResult result = session.run("MATCH(n {name:'联想'}) return n");
        while(result.hasNext()){
            Record record = result.next();
            System.err.println(record.get("n").get("name"));
        }

    }
    @Test
    public void testCreate(){
        Session session = driver.session();
        session.run("create (n:Computer1 {name:'联想',price:2800 })");
        session.run("create (n:Computer2 {name:'戴尔',price:6970 })");
        session.run("create (n:Computer3 {name:'宏碁',price:3220 })");
        session.run("create (n:Computer4 {name:'苹果',price:6800 })");
        StatementResult result = session.run("MATCH(n {name:'联想'}) return n");
        while(result.hasNext()){
            Record record = result.next();
            System.err.println(record.get("n").get("name"));
        }

    }

    @Test
    public void testDelete(){
        String sql="MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r";
        driver.session().run(sql);

    }
}
